/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.sun.dtv.ui;

import com.sun.dtv.lwuit.Display;
import com.sun.dtv.lwuit.geom.Dimension;
import com.sun.dtv.resources.ScarceResource;
import com.sun.dtv.resources.ScarceResourceListener;
import com.sun.dtv.resources.TimeoutException;
import java.util.Vector;

/**
 *
 * @author stupied / Leonardo Baptista (cbs)
 */
public class Screen implements ScarceResource  {

    /**
     * This is the instance of the EDT used internally to indicate whether
     * we are executing on the EDT or some arbitrary thread
     */ 
    private Thread edt;

    private int id;
    private Dimension dimension;
    private boolean isfree;
    
    static final Object lock = new Object();

    private static Screen[] screens;
    private static Screen defaultScreen;

    /**
     * Contains the call serially pending elements
     */
    private Vector pendingSerialCalls = new Vector();

    public void reserve(boolean force, long timeoutms, ScarceResourceListener listener) throws IllegalArgumentException, TimeoutException, SecurityException {
        if (force)
            isfree = true;
    }

    public void release() {
        isfree= true;
    }

    public boolean isAvailable() {
        return isfree;
    }

    protected static void initScreens(){
        int total = Screen.getNumberOfScreens();
        Screen[] xx = new Screen[total];
        for (int i=0;i<total;i++){
            xx[i] = new Screen();
            xx[i].id = i;
            xx[i].dimension = new Dimension(Screen.getWidthOfScreen(i), Screen.getHeightOfScreen(i));
            Display.init(Device.backend);
        }
        screens = xx;
    }

    private Screen(){
    }

    private static int getNumberOfScreens(){
        return 1;
    }

    private static int getWidthOfScreen(int id){
        return 640;
    }
    private static int getHeightOfScreen(int id){
        return 480;
    }
    
    //TODO descomentar esse metodo
    /*public static void addResourceTypeListener(ResourceTypeListener listener) {
    }*/
    
    public void addScarceResourceListener(ScarceResourceListener listener) {
    }

    public static Screen getCurrentScreen(){
        return null;
    }

    public static Screen getDefaultScreen(){
        return defaultScreen;
    }

    public static Screen[] getInstances(){
        return screens;
    }

     public Dimension getScreenAspectRatio() {
         return dimension;
     }
    /**
     * Returns true if we are currently in the event dispatch thread.
     * This is useful for generic code that can be used both with the
     * EDT and outside of it.
     * 
     * @return true if we are currently in the event dispatch thread; 
     * otherwise false
     */
    public boolean isEdt() {
        return edt == Thread.currentThread();
    }

    /**
     * Causes the runnable to be invoked on the event dispatch thread. This method
     * returns immediately and will not wait for the serial call to occur
     *
     * @param r runnable (NOT A THREAD!) that will be invoked on the EDT serial to
     * the paint and key handling events
     * @throws IllegalStateException if this method is invoked on the event dispatch thread (e.g. during
     * paint or event handling).
     */
    public void callSerially(Runnable r){
        if(isEdt()) {
            throw new IllegalStateException("Call serially must never be invoked from the EDT");
        }
        synchronized(lock) {
            pendingSerialCalls.addElement(r);
            lock.notify();
        }
    }

    /**
     * TODO: fazer metodo
     */
    public void callSeriallyAndWait(Runnable r){
    }
    
}
